// 聊天相关接口
import { request } from './httpBase';

// 消息类型定义
interface LastMessage {
  messageId: number;
  conversationId: number;
  senderId: number;
  content: string;
  messageType: number;
  mediaUrl: string;
  mediaDuration: number;
  userDeleted: number;
  workerDeleted: number;
  createTime: string;
}

// 会话信息定义
interface Conversation {
  conversationId: number;
  userId: number;
  workerId: number;
  userName: string;
  workerName: string;
  userAvatar: string;
  workerAvatar: string;
  userUnreadCount: number;
  workerUnreadCount: number;
  createTime: string;
  updateTime: string;
  lastMessage: LastMessage;
}

// 消息详情定义（用于获取消息列表）
interface Message {
  messageId: number;
  conversationId: number;
  senderId: number;
  content: string;
  messageType: number;
  mediaUrl: string;
  mediaDuration: number;
  userDeleted: number;
  workerDeleted: number;
  createTime: string;
}

// 发送消息请求参数定义
interface SendMessageParams {
  conversationId: number;
  messageType: number; // 1=文本,2=图片,3=语音
  content?: string;
  mediaUrl?: string;
  mediaDuration?: number;
}

// 会话基本信息定义（用于获取/创建会话接口）
interface ConversationInfo {
  conversationId: number;
  userId: number;
  workerId: number;
  createTime: string;
}

// 获取会话列表接口
export function getConversationList(params?: {
  pageNum?: number;
  pageSize?: number;
  roleType?: 'common' | 'worker';
}): Promise<{
  total: number;
  rows: Conversation[];
  code: number;
  msg: string;
}> {
  const requestData: any = {
    pageNum: params && params.pageNum ? params.pageNum : 1,
    pageSize: params && params.pageSize ? params.pageSize : 20
  };
  
  if (params && params.roleType) {
    requestData.roleType = params.roleType;
  }
  
  return request({
    url: '/chat/conversation/list',
    method: 'GET',
    data: requestData
  });
}

// 获取会话消息列表接口
export function getConversationMessages(
  conversationId: number,
  params?: {
    lastMsgId?: number;
    pageNum?: number;
    pageSize?: number;
  }
): Promise<{
  total: number;
  rows: Message[];
  code: number;
  msg: string;
}> {
  const requestData: any = {
    pageNum: params && params.pageNum ? params.pageNum : 1,
    pageSize: params && params.pageSize ? params.pageSize : 20
  };
  
  if (params && params.lastMsgId) {
    requestData.lastMsgId = params.lastMsgId;
  }
  
  return request({
    url: `/chat/conversation/${conversationId}/message`,
    method: 'GET',
    data: requestData
  });
}

// 发送消息接口
export function sendMessage(
  conversationId: number,
  messageData: SendMessageParams
): Promise<{
  code: number;
  msg: string;
  data: Message;
}> {
  return request({
    url: `/chat/conversation/${conversationId}/message`,
    method: 'POST',
    data: messageData
  });
}

// 获取用户与工人的聊天会话接口
export function getConversation(
  userId: number,
  workerId: number
): Promise<{
  code: number;
  msg: string;
  data: ConversationInfo;
}> {
  return request({
    url: `/chat/conversation/${userId}/${workerId}`,
    method: 'GET'
  });
}

// 创建用户与工人的聊天会话接口
export function createConversation(
  userId: number,
  workerId: number
): Promise<{
  code: number;
  msg: string;
  data: ConversationInfo;
}> {
  return request({
    url: `/chat/conversation/${userId}/${workerId}`,
    method: 'POST'
  });
}

// 删除会话接口
export function deleteConversation(
  conversationId: number
): Promise<{
  code: number;
  msg: string;
}> {
  return request({
    url: `/chat/conversation/${conversationId}`,
    method: 'DELETE'
  });
}

// 标记会话已读接口
export function markConversationRead(
  conversationId: number
): Promise<{
  code: number;
  msg: string;
}> {
  return request({
    url: `/chat/conversation/${conversationId}/read`,
    method: 'POST'
  });
}